package com.rp.system.controller;

import java.io.File;
import java.io.IOException;

import org.owasp.esapi.ESAPI;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.rp.common.utils.DownloadComponent;
import com.rp.common.utils.RpUtil;
import com.rp.controller.RpController;

@Controller
@RequestMapping("/rp/system/common/")
public class CommonController extends RpController {
	
	private final String SAVE_DIRECTORY = "exportDirectory";
	
	@RequestMapping("showSelectRolesWindow")
	public ModelAndView showSelectRolesWindow(String userId){
		if(userId != null){
			model.put("userId", userId);
		}
		return forward("/rp/system/common/selectRolesWindow.jsp");
	}
	
	@RequestMapping("showSelectResourcesWindow")
	public ModelAndView showSelectResourcesWindow(String authorityId){
		model.put("authorityId", authorityId);
		return forward("/rp/system/common/selectResourcesWindow.jsp");
	}
	
	@RequestMapping("showSelectAuthsWindow")
	public ModelAndView showSelectAuthsWindow(String roleId){
		model.put("roleId", roleId);
		return forward("/rp/system/common/selectAuthsWindow.jsp");
	}
	
	@RequestMapping("showOutlookMenu")
	public ModelAndView showOutlookMenu(String menuName){
		model.put("menuName", menuName);
		return forward("/rp/system/common/outlookmenu.jsp");
	}
	
	/**
	 * 使用ESAPI的encodeForHTML方法编码字符串
	 * @param str
	 * @return
	 */
	@ResponseBody
	@RequestMapping("encodeStr")
	public String encodeStr(String str){
		String newStr = ESAPI.encoder().encodeForHTML(str);
		//System.out.println("经过HTML编码后的字符串："+newStr);
		return newStr;
	}
	
	/**
	 * 生成一个GUID返回给前台
	 * @return
	 */
	@ResponseBody
	@RequestMapping("createGuid")
	public String createGuid(){
		return RpUtil.createGuid();
	}
	
	/**
	 * 返回项目所在的Tomcat路径给前端
	 * @return
	 */
	@ResponseBody
	@RequestMapping("getWebTomcatPath")
	public String getWebTomcatPath(){
		return this.getWebServerDirectory();
	}
	
	/**
	 * 将内容写入文件并保存在磁盘中
	 * @param jsonData
	 * @return
	 */
	@ResponseBody
	@RequestMapping("writeTxt")
	public String writeTxt(String jsonData){
		String date = RpUtil.getTodayDate(null);
		String fileName = date+"-"+RpUtil.createGuid();
		try {
			String exportDirectory = this.getWebServerDirectory()+"/"+SAVE_DIRECTORY+"/";
			File file = new File(exportDirectory);
			if(!file.exists()){
				file.mkdirs();
			}
			
			RpUtil.writeContent2File(exportDirectory+fileName+".txt", jsonData, false);
		} catch (IOException e) {
			e.printStackTrace();
			return ERROR;
		}
		return fileName;
	}
	
	/**
	 * 导出文件并下载
	 * @param fileName
	 */
	@ResponseBody
	@RequestMapping("exportFile")
	public void exportFile(String fileName){
		String exportDirectory = this.getWebServerDirectory()+"/"+SAVE_DIRECTORY+"/";
		String fileAbsolutePath = exportDirectory+fileName+".txt";
		try {
			
			DownloadComponent dc = new DownloadComponent(request, response);
			dc.downloadFile(fileAbsolutePath, fileName+".txt");
			
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			//下载完成后将文件从服务器上删除，以防恶意下载使服务器空间爆满
			File fi = new File(fileAbsolutePath);
			fi.delete();
		}
	}
	
}
